Skip to content

[Relax][Frontend][TFLite] Add BITCAST operator mapping#19466

Merged
tlopex merged 2 commits intoapache:mainfrom
as4230:feat/relax-bitcast
Apr 29, 2026
Merged

[Relax][Frontend][TFLite] Add BITCAST operator mapping#19466
tlopex merged 2 commits intoapache:mainfrom
as4230:feat/relax-bitcast

Conversation

@as4230
Copy link
Copy Markdown
Contributor

@as4230 as4230 commented Apr 29, 2026

This PR adds TFLite frontend support for the BITCAST operator which reinterprets a tensor's bytes as a different dtype without converting the underlying data.

The handler lowers BITCAST to relax.op.memory.view which aliases the input buffer with the new shape and dtype.

Frontend tests cover same-width (float32 -> int32, uint8 -> int8), width-changing smaller (int32[3] -> int16[3, 2]), and width-changing larger (int16[5, 2] -> int32[5]).

python -m pytest tests/python/relax/test_frontend_tflite.py -k bitcast -v

Addresses the BITCAST item under #19412.

This PR adds TFLite frontend support for the BITCAST operator which
reinterprets a tensor's bytes as a different dtype without converting
the underlying data.

The handler lowers BITCAST to relax.op.memory.view which aliases the
input buffer with the new shape and dtype.

Frontend regression tests cover same-width (float32 -> int32,
uint8 -> int8), width-changing smaller (int32[3] -> int16[3, 2]), and
width-changing larger (int16[5, 2] -> int32[5]).

` python -m pytest tests/python/relax/test_frontend_tflite.py -k bitcast -v `

Addresses the BITCAST item under apache#19412.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for the BITCAST operator in the TFLite frontend, mapping it to relax.op.memory.view. The implementation includes validation for input/output tensor counts and ensures total byte sizes match, with comprehensive tests added for various data type and shape transformations. Feedback recommends using the to_int_list utility for shape conversion to maintain consistency with the existing codebase and prevent potential type mismatches.

Comment thread python/tvm/relax/frontend/tflite/tflite_frontend.py Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@as4230
Copy link
Copy Markdown
Contributor Author

as4230 commented Apr 29, 2026

@tlopex - Quick design check. Issue #19412 lists BITCAST and mentions needing a dedicated op. I went with relax.op.memory.view since it's byte-preserving and zero-conversion which seems to match TFLite BITCAST.

If you'd prefer a first-class relax.bitcast op that legalizes to memory.view or if there's a semantic gap with memory.view I'm not seeing I'm happy to implement whatever's missing.

Copy link
Copy Markdown
Member

@tlopex tlopex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! No worries, your implementation is right @as4230

@tlopex tlopex merged commit 0e5c885 into apache:main Apr 29, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants